package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.SnowUtil;
import com.wyh.entity.AdminBlackList;
import com.wyh.entity.MsgMessage;
import com.wyh.mapper.MsgMessageMapper;
import com.wyh.request.MsgMessageRequest;
import com.wyh.service.IAdminBlackListService;
import com.wyh.service.IMsgMessageService;
import com.wyh.service.IMsgRecontactService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-08
 */
@Slf4j
@Service
@AllArgsConstructor
public class MsgMessageServiceImpl implements IMsgMessageService {

    private final MsgMessageMapper baseMapper;


    @Autowired
    private IAdminBlackListService adminBlackListService;

    @Autowired
    private IMsgRecontactService iMsgRecontactService;
    @Autowired
    private SnowUtil snowUtil;

    @Override
    public Boolean isOutThree(String fromId, String toId) {
        Long aLong = baseMapper.selectCount(Wrappers.lambdaQuery(MsgMessage.class)
                .eq(MsgMessage::getFromId, fromId)
                .eq(MsgMessage::getToId, toId));
        if (aLong>=3){
            return true;
        }
        return false;
    }

    @Override
    public R<String> sendMessage(MsgMessageRequest msgMessageRequest) {

        MsgMessage build = MsgMessage.builder().build();
        BeanUtils.copyProperties(msgMessageRequest,build);

        //判断是否被拉黑
        if (adminBlackListService.isBlack(msgMessageRequest.getFromId(), msgMessageRequest.getToId())){
            return R.error("被拉黑,无法发送信息");
        }

        //判断是否有好友
        if (!iMsgRecontactService.isFriend(msgMessageRequest.getFromId(), msgMessageRequest.getToId())){
            if (isOutThree(msgMessageRequest.getFromId(), msgMessageRequest.getToId())){
                return R.error("不是好友,且消息超过3条");
            }
            //陌生人发送
            long l = snowUtil.generateId();
            build.setMsgId(l);
            baseMapper.insert(build);
            return R.success("发送成功！");
        }


        //正常发送
        long l = snowUtil.generateId();
        build.setMsgId(l);
        baseMapper.insert(build);
        return R.success("发送成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MsgMessage add(MsgMessageRequest request) {
        MsgMessage entity = MsgMessage.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MsgMessageRequest request) {
        MsgMessage entity = MsgMessage.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public MsgMessage getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<MsgMessage> pageList(MsgMessageRequest request) {
        Page<MsgMessage> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<MsgMessage> query = this.buildWrapper(request);
        IPage<MsgMessage> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<MsgMessage> list(MsgMessageRequest request) {
        LambdaQueryWrapper<MsgMessage> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<MsgMessage> buildWrapper(MsgMessageRequest param) {
        LambdaQueryWrapper<MsgMessage> query = new LambdaQueryWrapper<>();

        if (param.getMsgType() != null){
               query.eq(MsgMessage::getMsgType ,param.getMsgType());
        }

         if (param.getMessageSendTime() != null){
               query.like(MsgMessage::getMessageSendTime ,param.getMessageSendTime());
        }
         if (StringUtils.isNotBlank(param.getContent())){
               query.like(MsgMessage::getContent ,param.getContent());
        }

        if (param.getCreateUserId() != null){
               query.eq(MsgMessage::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(MsgMessage::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
